package blog.ideablog.service;

import blog.ideablog.BlogApplication;
import blog.ideablog.dao.ArticleMapper;
import blog.ideablog.model.entity.ArticlePO;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Random;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {BlogApplication.class})
public class ArticleServiceTest {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleService articleService;

    // 查出所以数据，将创建、更新时间上下浮动5年，再写回数据库。
//    @Test
//    public void solveTime() {
//        List<ArticlePO> articles = articleMapper.selectList(null);
//        for (int i = 0; i < articles.size(); i++) {
//            ArticlePO po = articles.get(i);
//            LocalDateTime dateTime = LocalDateTime.now().plusYears(RandomUtil.randomInt(-5, 5));
//            long epochMilli = dateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
//            po.setGmtCreate(epochMilli);
//            po.setGmtUpdate(epochMilli);
//            articleMapper.updateById(po);
//        }
//    }

    @Test
    public void insArticle() {
        ArticlePO article = articleMapper.selectById("63be798a9d82f470469fc9be");
        for (int i = 0; i < 20; i++) {
            article.setId(IdUtil.objectId());
            article.setTitle("测试数据" + i);
            articleMapper.insert(article);
        }
    }

    @Test
    public void solveTime() {
        List<ArticlePO> articles = articleMapper.selectList(null);
        for (int i = 0; i < articles.size(); i++) {
            ArticlePO po = articles.get(i);
            LocalDateTime dateTime = LocalDateTime.now().plusYears(RandomUtil.randomInt(-5, 5));
            long epochMilli = dateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
            po.setGmtCreate(epochMilli);
            po.setGmtUpdate(epochMilli);
            articleMapper.updateById(po);
        }
    }

    @Test
    public void timeline() {
        articleService.timeline();
    }

}
